﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;

namespace ZhangZhao.WebAPI
{
    [System.ComponentModel.Description("被UploadAvatar.ashx取代")]
    public partial class UserAvatarEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = Output.Fail;

            try
            {
                string DataValue = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.JsonData]);
                string Command = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Command]);

                if (!string.IsNullOrWhiteSpace(DataValue))
                {
                    ZhangZhao.Model.H_UserAvatar model = (ZhangZhao.Model.H_UserAvatar)JsonConvert.DeserializeObject(DataValue);

                    if (model != null && !string.IsNullOrWhiteSpace(model.UserName) && !string.IsNullOrWhiteSpace(model.Avatar) && !string.IsNullOrWhiteSpace(Command))
                    {
                        ZhangZhao.BLL.H_UserAvatar bll = new ZhangZhao.BLL.H_UserAvatar();

                        if (Command.Equals(CommandParameterValue.Upload, StringComparison.CurrentCultureIgnoreCase))
                        {
                            #region 上传头像: 目前正在使用的头像设置为无效，添加新头像。
                            List<ZhangZhao.Model.H_UserAvatar> list = bll.GetModelList(string.Format(" UserName = '{0}' and IsUser = 1 ", model.UserName));

                            if (list != null && list.Count > 0)
                            {
                                foreach (ZhangZhao.Model.H_UserAvatar m in list)
                                {
                                    m.IsUse = false;
                                    bll.Update(m);
                                }
                            }

                            model.IsUse = true;
                            model.CreatedTime = DateTime.Now;
                            model.ViewCount = 1;
                            bll.Add(model);

                            s = Output.Success;

                            #endregion
                        }
                        else if (Command.Equals(CommandParameterValue.Update, StringComparison.CurrentCultureIgnoreCase))
                        {
                            #region 修改头像：正在使用的头像设为无效，插入新的头像

                            ZhangZhao.Model.H_UserAvatar CurrentModel = bll.GetModel(model.UserName);
                            if (CurrentModel != null)
                            {
                                CurrentModel.IsUse = false;
                                bll.Update(CurrentModel);
                            }

                            bll.Add(new ZhangZhao.Model.H_UserAvatar()
                            {
                                Avatar = model.Avatar,
                                AvatarLarge = model.AvatarLarge,
                                AvatarThumb = model.AvatarThumb,
                                CreatedTime = DateTime.Now,
                                IsUse = true,
                                UserName = model.UserName,
                                ViewCount = 1
                            });

                            s = Output.Success;

                            #endregion
                        }
                        else if (Command.Equals(CommandParameterValue.Delete, StringComparison.CurrentCultureIgnoreCase))
                        {
                            #region 删除头像：数据库删除记录，物理图片保留
                            
                            bll.Delete(model.Id);
                            s = Output.Success;
                            #endregion
                        }
                    }
                }
            }
            catch
            {
                s = Output.Exception;
            }

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}